/**
 * 添加产品类型
 */
layui.use(['layer', 'form', 'admin', 'ax', 'inputTags', 'laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var laydate = layui.laydate;


    //日期时间选择器
    laydate.render({
        elem: '#establishTime'
        ,type: 'datetime'
    });

    //处理数值
     handleNumber = function(value) {
        value = value.replace(/[^\d]/g, '').replace(/^0{1,}/g, '');
        if (value != '')
            value = parseFloat(value).toFixed(2);
        else
            value = parseFloat(0).toFixed(2);
        return value;
    }

    $("#city").hide();
    $("#region").hide();

    //加载省份列表
    $.ajax({
        url: Feng.ctxPath + '/area/list',
        dataType: 'json',
        type: 'get',
        data: {areaLevel: "101"},
        success: function (data) {
            $.each(data.data, function (index, value) {
                $('#provinceCode').append(new Option(value.areaName,value.areaCode));// 下拉菜单里添加元素
            });
            layui.form.render('select');
        }
    });


    form.on('select(provinceCode)', function (data) {
        var code = $("select[name=provinceCode]").val();
        if(code == ''){
            $("#city").hide();
            $("#region").hide();
        }else{
            $("#city").show();
            $.ajax({
                url: Feng.ctxPath + '/area/list',
                dataType: 'json',
                type: 'get',
                data: {parentAreaCode: code},
                success: function (data) {
                    $.each(data.data, function (index, value) {
                        $('#cityCode').append(new Option(value.areaName,value.areaCode));// 下拉菜单里添加元素
                    });
                    layui.form.render('select');
                }
            });

        }

        console.log("选中:"+code);
    });

    form.on('select(cityCode)', function (data) {
        var code = $("select[name=cityCode]").val();
        if(code == ''){
            $("#region").hide();
        }else{
            $("#region").show();
            $.ajax({
                url: Feng.ctxPath + '/area/list',
                dataType: 'json',
                type: 'get',
                data: {parentAreaCode: code},
                success: function (data) {
                    $.each(data.data, function (index, value) {
                        $('#regionCode').append(new Option(value.areaName,value.areaCode));// 下拉菜单里添加元素
                    });
                    layui.form.render('select');
                }
            });

        }

        console.log("选中:"+code);
    });






    // 表单提交事件
    form.on('submit(formSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/company/add", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });
});